package com.realestate.services;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.realestate.constants.ComTxManagerName;
import com.realestate.dao.ProductAttributeValueDao;
import com.realestate.model.BrtbProductAttributeValue;

@Service
public class ProductAttributeValueServiceImpl implements ProductAttributeValueService{
	
	@SuppressWarnings("unused")
	private static final Logger LOGGER = Logger.getLogger(ProductAttributeValueServiceImpl.class);
	
	@Autowired
    private ProductAttributeValueDao productAttributeDao;
 
	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public void persist(BrtbProductAttributeValue attribute) {
		productAttributeDao.persist(attribute);
		productAttributeDao.flush();
	}

	@Override
	public List<Object[]> getAllAttributesOfAProduct(Long productId) {
		return productAttributeDao.getAllAttributesOfAProduct(productId);
	}

	@Override
	public void deleteAllAttributeValueOfAProduct(Long productId) {
		List<BrtbProductAttributeValue> result = productAttributeDao.deleteAllAttributeValueOfAProduct(productId);
		for(BrtbProductAttributeValue value: result){
			productAttributeDao.removeById(value.getId());
		}
	}

	@Override
	public BrtbProductAttributeValue getByProductIdAndAttributeId(
			Long productId, Long attributeId) {
		return productAttributeDao.getByProductIdAndAttributeId(productId, attributeId);
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public BrtbProductAttributeValue merge(BrtbProductAttributeValue instance) {
		BrtbProductAttributeValue result = productAttributeDao.merge(instance);
		productAttributeDao.flush();
		return result;
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public void deleteByProductId(Long productId) {
		List<BrtbProductAttributeValue> values = productAttributeDao.deleteAllAttributeValueOfAProduct(productId);
		for(BrtbProductAttributeValue value : values){
			productAttributeDao.remove(value);
		}
	}
}
